import {
    createBtnHandler
} from './create';
import {
    editBtnHandler
} from './edit';
import {
    removeBtnHandler
} from './remove';
import {
    grid
} from '../common/grid';
import {
    editPwd,loginOut
} from '../common/settings';

$(function () {

    grid();
    window.editPwd=editPwd;
    window.loginOut=loginOut;

    let $table = $('#table').bootstrapTable({
        toolbar: '#toolbar',
        url: GLOBAL_prolistUrl,
        sidePagination: "server",
        queryParamsType: '',
        queryParams: function (params) {
            let query = $.extend({}, params,{cid:GLOBAL_cateid});
            return query;

        },
        detailView: true,
        detailFormatter: function (index, row, element) {
            let markup = ``;
            if(row.type == 1){
                markup = `<ul class="tag-box">
                              <li>中文产品说明：${row.explain_zh}</li>
                              <li>英文产品说明：${row.explain_en}</li>
                              <li>产品精度：${row.accuracy}</li>
                          </ul>`;
            }else{
                markup = `<ul class="tag-box">
                              <li>品牌：${row.brand}</li>
                              <li>产地(中文)：${row.local_zh}</li>
                              <li>产地(英文)：${row.local_en}</li>
                              <li>数量：${row.number}</li>
                          </ul>`;
            }

            return markup;
        },
        columns: [{
            checkbox: true
        }, {
            field: 'cate',
            title: '中/英产品分类名',
            sortable: true,
            formatter: function (value, row, index) {
                return row.czname+"  ||  "+row.cename;
            },
        }, {
            field: 'name_zh',
            title: '产品中文名',
            sortable: true
        }, {
            field: 'name_en',
            title: '产品英文名',
            sortable: true
        }, {
            field: 'version_zh',
            title: '中文产品型号',
            sortable: true
        }, {
            field: 'version_en',
            title: '英文产品型号',
            sortable: true
        }, {
            field: 'views',
            title: '查看次数',
            sortable: true
        }, {
            field: 'type',
            title: '类型',
            sortable: true,
            formatter: function (value, row, index) {
                return value == 1 ? "产品":"设备";
            },
        }, {
            field: 'update_time',
            title: '更新时间',
            sortable: true
        }]
    });


     window.createBtnHandler = createBtnHandler;
     window.editBtnHandler = editBtnHandler;
     window.removeBtnHandler = removeBtnHandler;

    //多选不能编辑
    $table.on('check.bs.table', () => {
        let selectedList = $('#table').bootstrapTable('getSelections');
        if (selectedList.length > 1) {
            $('#editBtn').attr('disabled', 'disabled');
            return;
        }
        $('#editBtn').removeAttr('disabled');
    });
    $table.on('uncheck.bs.table', () => {
        let selectedList = $('#table').bootstrapTable('getSelections');
        if (selectedList.length > 1) {
            $('#editBtn').attr('disabled', 'disabled');
            return;
        }
        $('#editBtn').removeAttr('disabled');
    });
    $table.on('check-all.bs.table', (rows) => {
        $('#editBtn').attr('disabled', 'disabled');
    });
    $table.on('uncheck-all.bs.table', (rows) => {
        $('#editBtn').removeAttr('disabled');
    });



});